home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / umodem40.c < prev    next >
Text File  |  1990-07-30  |  46KB  |  1,194 lines

  1. /*
  2.  *  UMODEM Version 4.0
  3.  *
  4.  *  UMODEM -- Implements the "CP/M User's Group XMODEM" protocol, 
  5.  *            the TERM II File Transfer Protocol (FTP) Number 1,
  6.  *            and the TERM II File Transfer Protocol Number 4 for
  7.  *            packetized file up/downloading.    
  8.  *
  9.  *    Note: UNIX System-Dependent values are indicated by the string [SD]
  10.  *          in a comment field on the same line as the values.
  11.  *
  12.  *
  13.  *         -- Lauren Weinstein, 6/81
  14.  *         -- (Version 2.0) Modified for JHU/UNIX by Richard Conn, 8/1/81
  15.  *         -- Version 2.1 Mods by Richard Conn, 8/2/81
  16.  *              . File Size Included on Send Option
  17.  *         -- Version 2.2 Mods by Richard Conn, 8/2/81
  18.  *              . Log File Generation and Option Incorporated
  19.  *         -- Version 2.3 Mods by Richard Conn, 8/3/81
  20.  *              . TERM II FTP 1 Supported
  21.  *              . Error Log Reports Enhanced
  22.  *              . CAN Function Added to FTP 3
  23.  *              . 'd' Option Added to Delete umodem.log File before starting
  24.  *         -- Version 2.4 Mods by Richard Conn, 8/4/81
  25.  *              . 16K-extent sector number check error corrected
  26.  *              . Count of number of received sectors added
  27.  *         -- Version 2.5 Mods by Richard Conn, 8/5/81
  28.  *              . ARPA Net Flag added
  29.  *              . ARPA Net parameter ('a') added to command line
  30.  *              . ARPA Net BIS, BIE, BOS, BOE added
  31.  *              . ARPA Net FFH escape added
  32.  *         -- Version 2.6 Mods by Bennett Marks, 8/21/81 (Bucky @ CCA-UNIX)
  33.  *              . mods for UNIX V7 (Note: for JHU compilation define
  34.  *                the variable JHU  during 'cc'
  35.  *              . added 'mungmode' flag to protect from inadvertant
  36.  *                overwrite on file receive
  37.  *              . changed timeout handling prior to issuing checksum
  38.  *         -- Version 2.7 Mods by Richard Conn, 8/25/81 (rconn @ BRL)
  39.  *              . correct minor "ifndef" error in which ifndef had no arg
  40.  *              . restructured "ifdef" references so that other versions
  41.  *                of UNIX than Version 7 and JHU can be easily incorporated;
  42.  *                previous ifdef references were for JHU/not JHU;
  43.  *                to compile under Version 7 or JHU UNIX, the following
  44.  *                command lines are recommended:
  45.  *                      "cc umodem.c -o umodem -DVER7" for Version 7
  46.  *                      "cc -7 umodem.c -o umodem -DJHU" for JHU
  47.  *              . added 'y' file status display option; this option gives
  48.  *                the user an estimate of the size of the target file to
  49.  *                send from the UNIX system in terms of CP/M records (128
  50.  *                bytes) and Kbytes (1024 byte units)
  51.  *              . added '7' option which modifies the transmission protocols
  52.  *                for 7 significant bits rather than 8; modifies both FTP 1
  53.  *                and FTP 3
  54.  *         -- Version 2.8 Mods by Richard Conn, 8/28/81
  55.  *              . corrected system-dependent reference to TIOCSSCR (for
  56.  *                disabling page mode) and created the PAGEMODE flag which
  57.  *                is to be set to TRUE to enable this
  58.  *              . added -4 option which engages TERM II, FTP 4 (new release)
  59.  *         -- Version 2.9 Mods by Richard Conn, 9/1/81
  60.  *              . internal documentation on ARPA Net protocols expanded
  61.  *              . possible operator precedence problem with BITMASK corrected
  62.  *                by redundant parentheses
  63.  *         -- Version 3.0 Mods by Lauren Weinstein, 9/14/81
  64.  *              . fixed bug in PAGEMODE defines (removed PAGEMODE define
  65.  *                line; now should be compiled with "-DPAGEMODE" if
  66.  *                Page Mode is desired)
  67.  *              . included forward declaration of ttyname() to avoid problems
  68.  *                with newer V7 C compilers
  69.  *         -- Version 3.1 Mods by Lauren Weinstein, 4/17/82
  70.  *              . avoids sending extraneous last sector when file EOF
  71.  *                occurs on an exact sector boundary
  72.  *         -- Version 3.2 Mods by Michael M Rubenstein, 5/26/83
  73.  *              . fixed bug in readbyte.  assumed that int's are ordered
  74.  *                from low significance to high
  75.  *              . added LOGDEFAULT define to allow default logging to be
  76.  *                off.  compile with -DLOGDEFAULT=0 to set default to no
  77.  *                logging.
  78.  *         -- Version 3.3 Mod by Ben Goldfarb, 07/02/83
  79.  *              . Corrected problem with above implementation of "LOGDEFAULT".
  80.  *                A bitwise, instead of a logical negation operator was
  81.  *                used to complement LOGFLAG when the '-l' command line
  82.  *                flag was specified.  This can cause LOGFLAG to be true
  83.  *                when it should be false.
  84.  *         -- Version 3.4 Mods by David F. Hinnant, NCECS, 7/15/83
  85.  *              . placed log file in HOME directory in case user doesn't
  86.  *                have write permission in working directory.
  87.  *              . added DELDEFAULT define to allow default purge/no purge
  88.  *                of logfile before starting.  Compile with -DDELDEFAULT=0
  89.  *                to set default to NOT delete the log file before starting.
  90.  *              . check log file for sucessful fopen().
  91.  *              . buffer disk read for sfile().
  92.  *              . turn messages off (standard v7) before starting.
  93.  *         -- Version 3.5 Mods by Richard Conn, 08/27/83
  94.  *              . added equates for compilation under UNIX SYSTEM III
  95.  *                      to compile for SYSTEM III, use -DSYS3 instead of
  96.  *                      -DJHU or -DVER7
  97.  *              . added command mode (-c option) for continuous entry
  98.  *                      of commands
  99.  *      --  Version 4.0 Mods by Mycroft Holmes, 04/17/84
  100.  *        . released constraint on using caps for commands in
  101.  *            the 'command' mode. 
  102.  *        . added descriptors for local mode, allowing use of
  103.  *            environmental variable 'MODEM' to specify
  104.  *            i/o port. now, no more limitation to remote
  105.  *            usage only.
  106.  *        . added rudamentary terminal program for local mode
  107.  *            and (ala modem7 series, thankyou
  108.  *            WC) ^E exits terminal mode.  
  109.  *        . reassigned SIGINT to command() when in local mode, thus
  110.  *            no die from program, just return to command mode.
  111.  *        . put the conditional (on local mode) display of sector
  112.  *            counts (so you could see it work).
  113.  *        . made case conversions to lower case conditional on
  114.  *            being upper case, instead of always.  thus using
  115.  *            lower case in commands works.  (defined 'mklow')
  116.  *        . put in flag to search for in order to get past all this
  117.  *            garbage at the start of the file.
  118.  *        . forced exit(0) on 'x' from command mode.
  119.  *EOS
  120.  */
  121.  
  122. #include <stdio.h>
  123. #include <sys/types.h>
  124. #include <sys/stat.h>
  125. #include <ctype.h>
  126. #define mklow(c) (isupper(c) ? tolower(c) : c)
  127.  
  128. #include <sgtty.h>
  129.  
  130. /* log default define */
  131. #ifndef LOGDEFAULT
  132. #define LOGDEFAULT      1
  133. #endif
  134.  
  135. /* Delete logfile define.  Useful on small systems with limited
  136.  * filesystem space and careless users.
  137.  */
  138. #ifndef DELDEFAULT
  139. #define DELDEFAULT      1
  140. #endif
  141.  
  142. #include <signal.h>
  143.  
  144. #define      VERSION    40      /* Version Number */
  145. #define      FALSE      0
  146. #define      TRUE       ~FALSE
  147.  
  148. /*  Compile with "-DPAGEMODE" if Page Mode (TIOCSSCR) is supported on your
  149.  *  UNIX system.  If it is supported, make sure that TIOCSSCR is the
  150.  *  correct name for Page Mode engagement.
  151.  */
  152.  
  153. /*  ASCII Constants  */
  154. #define      SOH        001 
  155. #define      STX        002
  156. #define      ETX        003
  157. #define      EOT        004
  158. #define      ENQ        005
  159. #define      ACK        006
  160. #define      LF         012   /* Unix LF/NL */
  161. #define      CR         015  
  162. #define      NAK        025
  163. #define      SYN        026
  164. #define      CAN        030
  165. #define      ESC        033
  166. #define      CTRLZ      032   /* CP/M EOF for text (usually!) */
  167.  
  168. /*  UMODEM Constants  */
  169. #define      TIMEOUT    -1
  170. #define      ERRORMAX   10    /* maximum errors tolerated */
  171. #define      RETRYMAX   10    /* maximum ret